import React, { useEffect } from "react"
import IndexRouter from "./router"
import "./App.css"
import { useHistory } from "react-router-dom"
// import store from "./store"
import { userType } from "./types"
import { http } from "./utils/http"
import { connect, ConnectedProps } from "react-redux"
import { setUserInfo } from "./store/actions/user"

type Props = ConnectedProps<typeof connector>

const App: React.FC<Props> = (props) => {
  const { setUserInfo } = props

  const history = useHistory()

  useEffect(() => {
    const token = sessionStorage.getItem("token")
    if (!token) return history.replace("/login")

    http
      .post("/get/user/info")
      .then(({ data: res }) => {
        // store.dispatch({ type: "set", data: res.data })

        setUserInfo(res.data)
      })
      .catch(() => {})

    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  return (
    <div className="App">
      <IndexRouter></IndexRouter>
    </div>
  )
}

const connector = connect((state: userType) => ({ userInfo: state }), { setUserInfo })

export default connector(App)
